package com.mycompany.myapp.service.impl;

import com.mycompany.myapp.service.AdvertisingService;
import com.mycompany.myapp.domain.Advertising;
import com.mycompany.myapp.repository.AdvertisingRepository;
import com.mycompany.myapp.service.dto.AdvertisingDTO;
import com.mycompany.myapp.service.mapper.AdvertisingMapper;
import com.mycompany.myapp.web.rest.utils.ResultObj;
import com.mycompany.myapp.web.rest.utils.TypeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing {@link Advertising}.
 */
@Service
@Transactional
public class AdvertisingServiceImpl implements AdvertisingService {

    private final Logger log = LoggerFactory.getLogger(AdvertisingServiceImpl.class);

    private final AdvertisingRepository advertisingRepository;

    private final AdvertisingMapper advertisingMapper;

    public AdvertisingServiceImpl(AdvertisingRepository advertisingRepository, AdvertisingMapper advertisingMapper) {
        this.advertisingRepository = advertisingRepository;
        this.advertisingMapper = advertisingMapper;
    }

    /**
     * Save a advertising.
     *
     * @param advertisingDTO the entity to save.
     * @return the persisted entity.
     */
    @Override
    public AdvertisingDTO save(AdvertisingDTO advertisingDTO) {
        log.debug("Request to save Advertising : {}", advertisingDTO);
        Advertising advertising = advertisingMapper.toEntity(advertisingDTO);
        advertising = advertisingRepository.save(advertising);
        return advertisingMapper.toDto(advertising);
    }

    /**
     * Get all the advertisings.
     *
     * @return the list of entities.
     */
    @Override
    @Transactional(readOnly = true)
    public List<AdvertisingDTO> findAll() {
        log.debug("Request to get all Advertisings");
        return advertisingRepository.findAll().stream()
            .map(advertisingMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }


    /**
     * Get one advertising by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<AdvertisingDTO> findOne(Long id) {
        log.debug("Request to get Advertising : {}", id);
        return advertisingRepository.findById(id)
            .map(advertisingMapper::toDto);
    }

    /**
     * Delete the advertising by id.
     *
     * @param id the id of the entity.
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Advertising : {}", id);
        advertisingRepository.deleteById(id);
    }

    public ResultObj updateAdvertising(AdvertisingDTO advertisingDTO){
        if(!TypeUtils.isEmpty(advertisingDTO)){
            save(advertisingDTO);
            return ResultObj.back(200,"更新成功！",null);
        }
        return  ResultObj.back(200,"更新失败！",null);
    }

    public ResultObj createAdvertising(AdvertisingDTO advertisingDTO){
        if(!TypeUtils.isEmpty(advertisingDTO)){
            save(advertisingDTO);
            return ResultObj.back(200,"插入成功！",null);
        }
        return  ResultObj.back(200,"插入失败！",null);
    }
}
